Volume 1: Fundamental Algorithms, Third Edition
Chapter 1.1: Algorithms
Exercise 1 [10]
The text showed how to interchange the values of variables and , using the replacement notation, by setting , , . Show how the values of four variables can be rearranged to by a sequence of replacements. Try to use the minimum number of replacements.
Solution
I am 99% sure that doing the above can be done in at minimum three switches, which I think are fairly obvious.
to get
to get
to get
Corrections
I read the problem wrong, it was asking for replacements instead of replacement .
Solution
Basically, put use as a single placeholder variable and shift everything within around:
to get and
to get and
to get and
to get and
to get
Corrections
N/A
Exercise 2 [15]
Prove that is always greater than at the beginning of Step E1, except possibly the first time this step occurs
Solution
Either , , or . For each of these situations:
-
1.
Assume that . In this case, the solution is trivial.
-
2.
Assume that . We execute Step E1 and get a remainder . At Step E2, the algorithm terminates so we never hit Step E1 again.
-
3.
Assume that . We execute Step E1 and get the remainder of . We execute Step E3 and set and . Now, has the value of the original and has the value of the original . Since and have switched, we get the new relationship of .
Corrections
N/A
Exercise 3 [20]
Change Algorithm E (for the sake of efficiency) so that all trivial replacement operations such as “” are avoided.
Solution
The following algorithm eliminates all trivial replacement operations:
-
1.
Divide by and let be the remainder
-
2.
If is zero, the algorithm terminates; is the answer
-
3.
Divide by and let be the remainder
-
4.
If is zero, the algorithm terminates; is the answer
-
5.
Return to step F1
Corrections
N/A
Exercise 4 [16]
What is the GCD of 2166 and 6099?
Solution
For fun, let’s use the algorithm in Exercise 3 with and .
2166 | 6099 | 2166 | |
2166 | 6099 | 1767 | |
2166 | 1767 | 399 | |
399 | 1767 | 171 | |
399 | 171 | 57 | |
57 | 171 | 0 | |
57 | 0 | 0 |
So the GCD is 57
Corrections
N/A
Exercise 5 [12]
Show that the “Procedure for Reading This Set of Books” that appear after the preface actually fails to be a genuine algorithm on at least three of our five counts. Also mention some differences in format between it an Algorithm E.
Solution
The five counts are as follows:
-
1.
Finiteness: An algorithm must always terminate after a finite number of steps.
-
2.
Definiteness: Each step of an algorithm must be precisely defined; the actions to be carried out must be rigorously and unambiguously specified for each case.
-
3.
Input: An algorithm has zero or more inputs, quantities that are given to it initially before the algorithm begins, or dynamically as the algorithm runs.
-
4.
Output: An algorithm has one or more outputs, quantities that have a specified relation to the inputs
-
5.
Effectiveness: An algorithm is also generally expected to be effective, in the sense that its operations must all be sufficiently basic that they can in principle be done exactly and in a finite length of time by someone using pencil and paper.
The “Procedure for Reading This Set of Books” fails on counts 1, 2, and 4. The procedure is not finite because all 17 steps before the last either point to another step or move on to the next. The final step returns you to step 3, so the procedure will never end. The procedure is not definite because the steps leave some things to the discretion of the reader, which is the opposite of a precise definition. And finally, the procedure has no output, because it never ends.
And here are a few differences with Algorithm E:
-
•
The procedure lacks a formal end point, where Algorithm E uses a heavy vertical line
-
•
The procedure is informal and vague, while Algorithm E uses strict notation and language
Corrections
N/A
Exercise 6 [20]
What is , the average number of times step E1 is performed when .
Solution
Let’s compute some values for . According to the book, to find we just do the algorithm for to , count the number of times step E1 has been done, and divide by
-
•
For and . E1: . E3: and . E1: . End with 2
-
•
For and . E1: . E3: and . E1: . E3: and . E1: End with 3
-
•
For and . E1: . E3: and . E1: . E3: and . E1: . E3: and . E1: . End with 4
-
•
For and . E1: . E3: and . E1: . End with 2
-
•
For and . E1: . End with 1
The total number of times step E1 has been done is and divide by to get 2.4
Corrections
Silly mistake in .
Solution
It should be:
For and . E1: . E3: and . E1: . E3: and . E1: . End with 3
So the total should be and divide by to get 2.6
As some additional food for thought, why do we only need to look at values of up to ? Let’s say . Then E1: , E3: , . This is identical to , a pattern which will repeat for all values of . Essentially, if , the steps followed are identical for if had equaled to start with.
Corrections
N/A
Exercise 7 [HM21]
Let be the average number of times that step E1 is executed in Algorithm E, if is known and is allowed to range over all positive integers. Show that is well defined. Is in any way related to ?
Solution
Let’s first try some examples I wrote some Python code (TODO: link) to do the computation for values of from 1 to 1,000,000 with the following results:
Approximation | Expanded Numerator | ||
---|---|---|---|
1 | 1.999999 | 2 | 2 |
2 | 2.499997 | 5 | |
3 | 2.999995 | 3 | 9 |
4 | 2.999993 | 3 | 12 |
5 | 3.599991 | 18 | |
6 | 3.166656 | 19 | |
7 | 3.857129 | 27 | |
8 | 3.749985 | 30 | |
9 | 3.77776 | 34 | |
10 | 3.699981 | 33 | |
11 | 4.454523 | 49 | |
12 | 3.666641 | 44 | |
13 | 4.461512 | 58 | |
14 | 4.21425 | 59 | |
15 | 4.066637 | 61 | |
16 | 4.124969 | 66 | |
17 | 4.705847 | 80 | |
18 | 4.27774 | 77 | |
19 | 4.894699 | 93 | |
20 | 4.199961 | 84 |
Note, based on observation of as the maximum value of increases, I am very confident that the value of will always converge. Additionally, the problem statement implies that is well defined.
Corrections
TODO
Exercise 8 [M25]
Give an “effective” formal algorithm for computing the GCD of positive integers and by specifying as in Eqs. (3). Let the input be represented by the string , that is, ’s followed by ’s. Try to make your solution as simple as possible. [Hint: Use Algorithm E, but instead of division in step E1, set , .]
Solution
TODO
Corrections
TODO
Exercise 9 [M30]
Suppose that and are computational methods. For example might stand for Algorithm E as in Eqs. (2), except that and are restricted in magnitude, and might stand for a computer program implementation of Algorithm E. (Thus might be the set of all states of the machine, i.e., all possible configurations of its memory and registers; might be the definition of single machine actions; and might be the set of initial states, each including the program that determines the GCD as well as the particular values of and .)
Formulate a set-theoretic definition for the concept “ is a representation of ” or “ simulates ”. This is to mean intuitively that any computation sequence of is mimicked by , except that might take more steps in which to do the computation and it might retain more information in its states
Solution
TODO
Corrections
TODO